package com.ruihu.admin.module.business.node.config.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruihu.admin.common.constant.JudgeEnum;
import com.ruihu.admin.common.constant.ResponseCodeConst;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.constant.NodeConfigTypeEnum;
import com.ruihu.admin.constant.SystemStatusEnum;
import com.ruihu.admin.module.business.node.config.constant.TaskProcessConfigResponseCodeConst;
import com.ruihu.admin.module.business.node.config.dao.TaskProcessConfigDao;
import com.ruihu.admin.module.business.node.config.domain.dto.TaskProcessConfigAddDTO;
import com.ruihu.admin.module.business.node.config.domain.dto.TaskProcessConfigDTO;
import com.ruihu.admin.module.business.node.config.domain.dto.TaskProcessConfigQueryDTO;
import com.ruihu.admin.module.business.node.config.domain.dto.TaskProcessConfigUpdateDTO;
import com.ruihu.admin.module.business.node.config.domain.entity.TaskProcessConfigEntity;
import com.ruihu.admin.module.business.node.config.domain.vo.TaskProcessConfigExcelVO;
import com.ruihu.admin.module.business.node.config.domain.vo.TaskProcessConfigVO;
import com.ruihu.admin.module.system.user.UserService;
import com.ruihu.admin.module.system.user.domain.bo.UserBO;
import com.ruihu.admin.util.BeanUtil;
import com.ruihu.admin.util.PageUtil;
import com.ruihu.admin.util.RequestTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * [ 节点配置表 ]
 *
 * @author Tony
 * @version 1.0
 * @company 瑞虎（南京）信息技术有限公司
 * @copyright (c)  瑞虎（南京）信息技术有限公司Inc. All rights reserved.
 * @date 2022-12-27 16:32:13
 * @since JDK1.8
 */
@Service
public class TaskProcessConfigService {

    @Autowired
    private TaskProcessConfigDao taskProcessConfigDao;

    @Autowired
    private UserService userService;

    /**
     * 根据id查询
     */
    public TaskProcessConfigEntity getById(Long id){
        return  taskProcessConfigDao.selectById(id);
    }

    /**
     * 分页查询
     * @author Tony
     * @date 2022-12-27 16:32:13
     */
    public ResponseDTO<PageResultDTO<TaskProcessConfigVO>> queryByPage(TaskProcessConfigQueryDTO queryDTO) {
        Page page = PageUtil.convert2QueryPage(queryDTO);
        List<TaskProcessConfigVO> voList = taskProcessConfigDao.queryByPage(page, queryDTO).stream().map(this::buildTaskProcessConfigVO)
                .collect(Collectors.toList());
        page.setRecords(voList);
        PageResultDTO<TaskProcessConfigVO> pageResultDTO = PageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 查询动态节点配置
     * @return
     */
    public ResponseDTO<List<TaskProcessConfigVO>> queryDynamicConfigElementList() {
        List<TaskProcessConfigVO> dynamicConfigElementList = taskProcessConfigDao.queryDynamicConfigElementList();
        return ResponseDTO.succData(dynamicConfigElementList);
    }

    private TaskProcessConfigVO buildTaskProcessConfigVO(TaskProcessConfigVO taskProcessConfigVO) {
        if(taskProcessConfigVO == null){
            return null;
        }
        if (Objects.nonNull(taskProcessConfigVO.getCreator())) {
            UserBO createUser = this.userService.getById(taskProcessConfigVO.getCreator());
            if (Objects.nonNull(createUser)) {
                taskProcessConfigVO.setCreateByName(createUser.getActualName());
            }
        }
        if (Objects.nonNull(taskProcessConfigVO.getUpdater())) {
            UserBO updateUser = this.userService.getById(taskProcessConfigVO.getUpdater());
            if (Objects.nonNull(updateUser)) {
                taskProcessConfigVO.setUpdateByName(updateUser.getActualName());
            }
        }
        if(Objects.nonNull(taskProcessConfigVO.getType())){
            taskProcessConfigVO.setTypeName(NodeConfigTypeEnum.selectByValue(taskProcessConfigVO.getType()).getMessage());
        }
        if(Objects.nonNull(taskProcessConfigVO.getIsThrowDesc())){
            taskProcessConfigVO.setIsThrowDescName(SystemStatusEnum.selectByValue(taskProcessConfigVO.getIsThrowDesc()).getMessage());
        }
        if(Objects.nonNull(taskProcessConfigVO.getHasImage())){
            taskProcessConfigVO.setHasImageName(SystemStatusEnum.selectByValue(taskProcessConfigVO.getHasImage()).getMessage());
        }
        if (Objects.nonNull(taskProcessConfigVO.getInterfaceImage())) {
            taskProcessConfigVO.setInterfaceImageName(SystemStatusEnum.selectByValue(taskProcessConfigVO.getInterfaceImage()).getMessage());
        }
        if (Objects.nonNull(taskProcessConfigVO.getFixed())) {
            taskProcessConfigVO.setFixedDesc(SystemStatusEnum.selectByValue(taskProcessConfigVO.getFixed()).getMessage());
        }
        if (Objects.nonNull(taskProcessConfigVO.getIsOther())) {
            taskProcessConfigVO.setIsOtherDesc(SystemStatusEnum.selectByValue(taskProcessConfigVO.getIsOther()).getMessage());
        }
        return taskProcessConfigVO;
    }

    /**
     * 添加
     * @author Tony
     * @date 2022-12-27 16:32:13
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(TaskProcessConfigAddDTO addDTO) {

       /* TaskProcessConfigDTO taskProcessConfigDTO = taskProcessConfigDao.getByName(addDTO.getName());
        if (Objects.nonNull(taskProcessConfigDTO)) {
            return ResponseDTO.wrap(TaskProcessConfigResponseCodeConst.CONFIG_NAME_HAS_EXISTS);
        }*/
        TaskProcessConfigEntity entity = BeanUtil.copy(addDTO, TaskProcessConfigEntity.class);
        entity.setCreator(RequestTokenUtil.getRequestUser().getRequestUserId());
        entity.setCreateTime(new Date());
        entity.setIsDelete(JudgeEnum.NO.getValue());
        taskProcessConfigDao.insert(entity);
        return ResponseDTO.succ();
    }

    /**
     * 编辑
     * @author Tony
     * @date 2022-12-27 16:32:13
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(TaskProcessConfigUpdateDTO updateDTO) {
        /*TaskProcessConfigDTO taskProcessConfigDTO = taskProcessConfigDao.getByName(updateDTO.getName());
        if (Objects.nonNull(taskProcessConfigDTO)&&!Objects.equals(updateDTO.getId(),taskProcessConfigDTO.getId())) {
            return ResponseDTO.wrap(TaskProcessConfigResponseCodeConst.CONFIG_NAME_HAS_EXISTS);
        }*/
        TaskProcessConfigEntity entity = this.taskProcessConfigDao.selectById(updateDTO.getId());
        if (Objects.isNull(entity))
            return ResponseDTO.wrap(ResponseCodeConst.NOT_EXISTS);
        entity = TaskProcessConfigEntity.convertToTaskProcessConfigEntity(updateDTO);
        entity.setUpdater(RequestTokenUtil.getRequestUser().getRequestUserId());
        entity.setUpdateTime(new Date());
        this.taskProcessConfigDao.updateById(entity);
        return ResponseDTO.succ();
    }

    /**
     * 删除
     * @author Tony
     * @date 2022-12-27 16:32:13
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> idList) {
        taskProcessConfigDao.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    /**
     * 删除
     * @author Tony
     * @date 2022-12-27 16:32:13
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteById(Long id) {
        TaskProcessConfigEntity entity = this.taskProcessConfigDao.selectById(id);

        if (Objects.isNull(entity))
            return ResponseDTO.wrap(ResponseCodeConst.NOT_EXISTS);

        if (SystemStatusEnum.YES.getCode().equals(entity.getFixed())) {
            return ResponseDTO.wrap(ResponseCodeConst.PROCESS_NODE_CANNOT_DELETE);
        }
        taskProcessConfigDao.deleteById(id);
        return ResponseDTO.succ();
    }


    /**
     * 查询全部导出对象
     * @author Tony
     * @date 2022-12-27 16:32:13
     */
    public List<TaskProcessConfigExcelVO> queryAllExportData(TaskProcessConfigQueryDTO queryDTO) {
        return taskProcessConfigDao.queryAllExportData( queryDTO);
    }

    /**
     * 批量查询导出对象
     * @author Tony
     * @date 2022-12-27 16:32:13
     */
    public List<TaskProcessConfigExcelVO> queryBatchExportData(List<Long> idList) {
        return taskProcessConfigDao.queryBatchExportData(idList);
    }
}
